package by.koleda.benicetobunnies.helper;

import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.util.Log;

public class CompaniesHelper {
	
	public static String getCompanyNameFromHtml(String html) {
		try {
			Document doc = Jsoup.parse(html);
			Element table = doc.select("table[style=width:100%]").first();
			if (table != null) {
				Iterator<Element> iterator = table.select("td").iterator();
				while(iterator.hasNext()){
					String companyName = iterator.next().text();
					if (!isProtected(companyName)) {
						return companyName;
					} else {
						return "";
					}
				}
			}
			return "";
		} catch (Exception e) {
			Log.w("Jsoup parsing exception: ", e.getMessage());
			return "";
		}
	}
	
	public static boolean isProtected(String companyName) {
		if (companyName.length() > 3) {
			if (companyName.substring(0, 3).equals("GS1")) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}
